import service from '@/service';
export default {
    state: {
        conf: {},
        info: {},
        token: uni.getStorageSync('token') || '',
        app: uni.getStorageSync('app') || '',
        channelId: uni.getStorageSync('channelId') || '',
        pId: uni.getStorageSync('pId') || ''
    },

    actions: {
        getUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                service.wxjlj
                    .getUserInfo()
                    .then(res => {
                        commit('SET_USERINFO', res.user);
                        commit('SET_APP', res.user.app);
                        commit('SET_CHANNELID', res.user.channelId);
                        commit('SET_PID', res.user.id);
                        uni.setStorageSync('userInfo', res.user);
                        resolve(res);
                    })
                    .catch(err => {
                        uni.showToast({
                            title: err,
                            icon: 'none'
                        });

                        reject(err);
                    });
            });
        },
        getConf({ commit }) {
            return new Promise((resolve, reject) => {
                service.wxjlj
                    .getConfig()
                    .then(res => {
                        commit('SET_CONF', res);
                        resolve(res);
                    })
                    .catch(err => {
                        uni.showToast({
                            title: err,
                            icon: 'none'
                        });

                        reject(err);
                    });
            });
        },
        addVisitRecord({ state }) {
            service.wechatApplet
                .addVisitRecordUnAuthorized({
                    channelId: state.info.channelId || state.channelId || 1,
                    userId: uni.getStorageSync('uuid') || ''
                })
                .then(res => {
                    if (!uni.getStorageSync('uuid')) {
                        uni.setStorageSync('uuid', res.userId);
                    }
                });
        }
    },

    mutations: {
        SET_USERINFO(state, value) {
            // value.subsidiesAmount = 0;
            // uni.setStorageSync('subscribe-date', '2020-09-02 11:49:59');
            if (value.subsidiesAmount == '0.00') {
                value.subsidiesAmount = 0;
            }
            state.info = value;
        },
        BUY(state, price) {
            state.info.old_subsidiesAmount = state.info.subsidiesAmount;
            state.info.subsidiesAmount = (state.info.subsidiesAmount - (price || 0)).toFixed(2);
        },
        SET_CONF(state, value) {
            state.conf = value;
        },
        SET_TOKEN(state, value) {
            state.token = value;
            uni.setStorageSync('token', value);
        },
        CLEAR_TOKEN(state) {
            state.token = null;
            uni.removeStorageSync('token');
        },
        SET_APP(state, value) {
            state.app = value;
            uni.setStorageSync('app', value);
        },
        SET_CHANNELID(state, value) {
            state.channelId = value;
            uni.setStorageSync('channelId', value);
        },
        SET_PID(state, value) {
            state.pId = value;
            uni.setStorageSync('pId', value);
        },
        CLEAR_ALL(state) {
            state.info = {};
            state.conf = {};
            state.token = '';
            state.app = '';
            state.channelId = '';
            state.pId = '';
            uni.clearStorageSync();
        }
    }
};
